package com.heima.reggie.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.reggie.common.deleteException;
import com.heima.reggie.mapper.CategoryMapper;
import com.heima.reggie.pojo.Category;
import com.heima.reggie.pojo.Dish;
import com.heima.reggie.pojo.Setmeal;
import com.heima.reggie.service.CategoryService;
import com.heima.reggie.service.DishService;
import com.heima.reggie.service.SetmealService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;
    @Override
    public void delect(Long id ) {

        LambdaQueryWrapper<Dish> dish = new LambdaQueryWrapper<>();
        dish.eq(Dish::getCategoryId, id);

        int count = dishService.count(dish);
        log.info("打印行数{}",count);
        if (count > 0) {
            throw new deleteException("有关联的菜品");

        }

        LambdaQueryWrapper<Setmeal> set = new LambdaQueryWrapper<>();
        set.eq(Setmeal::getCategoryId, id);


        int count1 = setmealService.count(set);

        if (count1 > 0) {
            throw new deleteException("有关联的套餐");

        }

        super.removeById(id);
    }
}
